<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        $('#chrcount').keyup(function(){
            var len = $('#chrcount').val();
            var char = len.length;	
            if(254-char == 0){
            }
            $('#chrcountlength').html(255-char);	
        });
    });
	
    $(document).ready(function(){
        $('#CategoryPricePercentage').keypress(validateNumber);
        $('#CategoryMerchantPercentage').keypress(validateNumber);
        $('.mws-button-row > input.mws-button').live('click',function(){
            $("input").live('click',function(){
                $(this).removeClass('error');
                $(this).next().remove();
            });
            CategoryPricePercentage = $('#CategoryPricePercentage').val();
            CategoryMerchantPercentage = $('#CategoryMerchantPercentage').val();
            error = 0;
            if(CategoryPricePercentage.length == 0){
                $('#CategoryPricePercentage').next().remove();
                $('#CategoryPricePercentage').addClass('error');
                $('#CategoryPricePercentage').after('<p class="errorPercentage" style="position:absolute; color: red">Please insert a value</p>');
                error = 1;
            }else if(CategoryPricePercentage > 100 ){
                $('#CategoryPricePercentage').next().remove();
                $('#CategoryPricePercentage').addClass('error');
                $('#CategoryPricePercentage').after('<p class="errorPercentage" style="position:absolute; color: red">value must between 0 and 100</p>');
                error = 1;
            }
            if(CategoryMerchantPercentage.length == 0){
                $('#CategoryMerchantPercentage').next().remove();
                $('#CategoryMerchantPercentage').addClass('error');
                $('#CategoryMerchantPercentage').after('<p class="errorPercentage" style="position:absolute; ; color: red"> Please insert a value</p>');
                error = 1;
            }else if(CategoryMerchantPercentage > 100 ){
                $('#CategoryMerchantPercentage').next().remove();
                $('#CategoryMerchantPercentage').addClass('error');
                $('#CategoryMerchantPercentage').after('<p class="errorPercentage" style="position:absolute; color: red">value must be between 0 and 100</p>');
                error = 1;
            }
            if(error ==1){
                return false;
            }
        });
        $('#chrcount2').keyup(function(){
            var len = $('#chrcount2').val();
            var char = len.length;	
            if(254-char == 0){
            }
            $('#chrcountlength2').html(255-char);	
        });
		
        $('#removeimageclick').bind("click", function(){
            var val =   confirm('Are You Sure?');
            if(val==true){
                $('.cateditimg img').remove();
                $('#imgvavalu').attr('value', ' ');
                $('#removeimageclick').html('Image Removed.');
            }
            else
            {
                return false;
            }
        });
    });
    function validateNumber(event) {
        var key = window.event ? event.keyCode : event.which;

        if (event.keyCode == 8 || event.keyCode == 46
            || event.keyCode == 37 || event.keyCode == 39) {
            return true;
        }
        else if ( key < 48 || key > 57 ) {
            return false;
        }
        else return true;
    };
</script>
<div id="mws-container" class="clearfix">
    <div class="container">
        <div class="mws-panel grid_8">
            <div class="mws-panel-header"> <span class="mws-i-24 i-list">
                    <?php
                    if (isset($details['Category']['id']) and ($details['Category']['id'])) {
                        echo "Edit";
                    } else {
                        ?>
                        Add
                    <?php } ?>
                    Categories</span> </div>
            <div class="mws-panel-body">
                <div class="mws-form-inline"> 
                    <?php echo $form->create(array('controller' => 'setups', 'action' => '', 'method' => 'post', 'enctype' => 'multipart/form-data', 'class' => 'mws-form')); ?> 
                    <?php
                    if (isset($details['Category']['id'])) {
                        $details['Category']['id'] = $details['Category']['id'];
                    } else {
                        $details['Category']['id'] = "";
                    }
                    echo $this->Form->input('Category.id', array('type' => 'hidden', 'value' => $details['Category']['id']));
                    ?>
                    <div class="mws-form-row">
                        <label>* Title :</label>
                        <div class="mws-form-item small"> 
                            <?php
                            if (isset($details['Category']['name'])) {
                                $details['Category']['name'] = $details['Category']['name'];
                            } else {
                                $details['Category']['name'] = "";
                            }
                            echo $this->Form->input('Category.name', array('type' => 'text', 'value' => $details['Category']['name'], 'class' => 'mws-textinput', 'label' => false));
                            ?> 
                        </div>
                    </div>
                    <div class="mws-form-row">
                        <label>*WonderE Price Percentage(<strong>%</strong>) :</label>
                        <div class="mws-form-item small"> 
                            <?php
                            if (isset($details['Category']['price_percentage'])) {
                                $details['Category']['price_percentage'] = $details['Category']['price_percentage'];
                            } else {
                                $details['Category']['price_percentage'] = "";
                            }
                            echo $this->Form->input('Category.price_percentage', array('type' => 'text', 'value' => $details['Category']['price_percentage'],
                                'class' => 'mws-textinput', 'label' => false, 'placeholder' => 'Only Numeric e.g 13'));
                            ?> 
                        </div>
                    </div>
                    <div class="mws-form-row">
                        <label>* Seller Price Percentage(<strong>%</strong>) :</label>
                        <div class="mws-form-item small"> 
                            <?php
                            if (isset($details['Category']['merchant_percentage'])) {
                                $details['Category']['merchant_percentage'] = $details['Category']['merchant_percentage'];
                            } else {
                                $details['Category']['merchant_percentage'] = "";
                            }
                            echo $this->Form->input('Category.merchant_percentage', array('type' => 'text', 'value' => $details['Category']['merchant_percentage'],
                                'class' => 'mws-textinput', 'label' => false, 'placeholder' => 'Only Numeric e.g 15'));
                            ?> 
                        </div>
                    </div>
                    <div class="mws-form-row">
                        <label>Browse Image:</label>
                        <div class="mws-form-item 	small"> 
                            <?php echo $this->Form->input('Category.imgImage', array('type' => 'file', 'value' => '', 'class' => '', 'label' => false)); ?> </div>
                    </div>
                    <?php if (isset($details['Category']['id']) and ($details['Category']['id'])) { ?>
                        <div class="mws-form-row">
                            <label>Old Image:</label>
                            <div class="mws-form-item  cateditimg" > 
                                <?php
                                echo $html->image('Category/' . $details['Category']['img'], array('width' => '150', 'height' => '150'));

                                echo $this->Form->input('Category.imgvalue', array('type' => 'hidden', 'value' => '23', 'id' => 'imgvavalu'));
                                ?>
                                <span><a href="javascript:void(0);" id="removeimageclick">Remove Image</a></span> 
                            </div>
                        </div>
                    <?php } ?>

                    <div class="mws-form-row">
                        <label>description :</label>
                        <div class="mws-form-item small"> <?php
                    if (isset($details['Category']['description'])) {
                        $details['Category']['description'] = $details['Category']['description'];
                    } else {
                        $details['Category']['description'] = "";
                    }
                    echo $this->Form->input('Category.description', array('type' => 'textarea', 'value' => $details['Category']['description'], 'class' => '', 'label' => false));
                    ?> </div>
                    </div>
                    <div class="mws-form-row">
                        <label>Parent Type :</label>
                        <div class="mws-form-item small"> 
                            <?php
                            // if(isset($details['Category']['pid'])){$details['Category']['pid']=$details['Category']['pid'];}else{$details['Category']['pid']="";}
                            // echo $this->Form->input('Category.pid', 
                            // array('type' => 'select','options'=>$_list_categories,
                            //  'default'=>$details['Category']['pid'],'class'=>'mws-textinput','empty'=>false,'label' =>false));
                            ?> 

                            <?php
                            if ($_list_categories and $_list_categories) {
                                ?>
                                <select id="CategoryPid" class="mws-textinput" name="data[Category][pid]">
                                    <option value="0">Parent</option>
                                    <?php
                                    foreach ($_list_categories as $_Parent_cats) {
                                        ?>			
                                        <option value="<?php echo $_Parent_cats['Category']['id']; ?>" 
                                                class="cat_grp_1"><?php echo $_Parent_cats['Category']['name']; ?></option>
                                                <?php
                                                $ko = 0;
                                                foreach ($_Parent_cats['Category']['Sub_1_Cat'] as $Sub_1_Cat) {
                                                    echo "<option value=" . $Sub_1_Cat['Category']['id'] . " class='cat_grp_2'>
											&nbsp;&nbsp;&bull;&nbsp;&nbsp;&nbsp;" . $Sub_1_Cat['Category']['name'] . "
											</option>";
                                                    if (isset($Sub_1_Cat['Category']['Sub_2_cat']) and ($Sub_1_Cat['Category']['Sub_2_cat'])) {
                                                        $bn = 0;
                                                        $Sub_2_cats = $Sub_1_Cat['Category']['Sub_2_cat'][$ko];
                                                        foreach ($Sub_2_cats as $Sub_2_cat) {
                                                            echo "<option value=" . $Sub_2_cat['Category']['id'] . " class='cat_grp_3'>
															&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&loz;&nbsp;
															&nbsp;" . $Sub_2_cat['Category']['name'] . "</option>";
                                                            $bn++;
                                                        }
                                                    }
                                                    $ko++;
                                                }
                                                ?>
                                                <?php
                                            }
                                            ?>            
                                </select> 
                                <?php
                            }
                            ?>            

                        </div>
                    </div>
                    <div class="mws-form-row">
                        <label>Parent Type :</label>
                        <div class="mws-form-item small">
                            <?php
                            if (isset($details['Category']['status'])) {
                                $details['Category']['status'] = $details['Category']['status'];
                            } else {
                                $details['Category']['status'] = "";
                            }
                            $options = array('1' => 'Active', '0' => 'Deactive');
                            echo $this->Form->input('Category.status', array('type' => 'select', 'options' => $options,
                                'default' => $details['Category']['status'], 'class' => 'mws-textinput', 'empty' => false, 'label' => false)
                            );
                            ?>
                        </div>
                    </div>
                    <div class="mws-form-row">
                        <label>
                            <p id="chrcountlength2"></p>
                        </label>
                    </div>
                    <div class="mws-button-row">
                        <input type="submit" class="mws-button black" value="Submit"/>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
